package com.soso.confidant.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Remote {
	
	private static Remote remote = new Remote();
	
	private Remote(){
	}

	public static Remote getInstance(){
		return remote;
	}
	
	/**
	 * @param InputStream is
	 * @return String
	 * @throws IOException
	 */
	public String inputStream2String(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		return baos.toString();
	}
	
	/**
	 * @param String url,String charset
	 * @return String
	 * @throws IOException
	 */
	public String getRemotePage(String url,String charset) {
        String sText = "";
        URLConnection urlcon = null;
        try {
            URL XmlUrl = new URL(url);
            urlcon = XmlUrl.openConnection();
            InputStream in = urlcon.getInputStream();
            String line = "";
            InputStreamReader fileIn = new InputStreamReader(in,charset);
            BufferedReader reader = new BufferedReader(fileIn);
            while ((line = reader.readLine()) != null) {
                sText += line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sText;
    } 

}